<?php
namespace app\api\validate;


use app\common\validate\BaseValidate;
use app\common\model\task\TaskList;

class TaskValidate extends BaseValidate
{
    protected $rule = [
        'task_list_id' => 'require|integer',
        'task_id' => 'require|integer',
        'task' => 'require|length:3,250',
        'list' => 'require|array'
    ];

    protected $message = [
        'task_list_id.require'     => '参数缺失',
        'task_list_id.integer'     => '值不对',
        'task_list_id.checkTaskListOwner'     => '任务清单不存在或不属于你',
        'task.require'=>'任务不能为空',
        'task.length'=>'任务长度错误',
    ];

    public function sceneAddTask()
    {
        return $this->only(['task_list','task']);
    }

    public function sceneAddTaskBatch(){
        return $this->only(['list']);
    }

    public function sceneOpOwner(){
        return $this->only(['task_id','task_list_id'])->append('checkTaskListOwner');
    }

    public function sceneOpWoker(){
        return $this->only(['task_id','task_list_id'])->append('checkTaskListWoker');
    }

    public function sceneQueryTask(){
        return $this->only(['share_code']);
    }

    public function checkTaskListOwner($value,$rule,$data){
        if($data['task_list_id'] == 0){
            return true;
        }
        $model = new TaskList();
        $tasklist = $model->where('id','=',$data['task_list_id'])->find();
        if(empty($tasklist) || $tasklist->user_id != $data['user_id']){
            return '任务清单不存在或不属于你!';
        }
        return true;
    }


    public function checkTaskListWoker($value,$rule,$data){
        $model = new TaskList();
        $tasklist = $model->where('id','=',$data['task_list_id'])->find();
        if(empty($tasklist) || $tasklist->worker_id != $data['user_id']){
            return '任务清单不存在或不属于你!';
        }
        return true;
    }


    
}